package com.bee.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bee.web.mapper.UserEntityMapper;
import com.bee.web.util.PwdUtil;
import com.hengyuan.db.UserEntity;

/**
 * 用户相关业务处理
 *
 * @author liuzy
 * @date 2015年6月5日
 */
@Service
public class UserService {

    @Autowired
    private UserEntityMapper userEntityMapper;

    /**
     * 获取用户ID
     *
     * @param userName
     * @return
     */
    public String getUserIdByUserName(String userName) throws Exception {
        UserEntity userEntity = userEntityMapper.selectByUserName(userName);
        if (userEntity != null) {
            return userEntity.getUserId();
        } else {
            return null;
        }
    }
    
    /**
     * 登陆验证
     *     
     * @param bean
     * @return
     * @throws Exception
     */
    public boolean login(String username, String password) throws Exception {
    	UserEntity userEntity = userEntityMapper.selectByUserName(username);
		if (userEntity != null) {
			if (PwdUtil.validate(password, userEntity.getSalt(), userEntity.getPassword())) {
				return true;
			}
		}
		return false;
    }

    /**
     * 修改密码
     * 
     * @param bean
     * @return
     * @throws Exception
     */
    public boolean uPassword(String userId, String password) throws Exception {
        UserEntity entity = new UserEntity();
        entity.setUserId(userId);
        entity.setPassword(password);
        if (userEntityMapper.update(entity) > 0) {
            return true;
        } else {
            return false;
        }
    }
    
}
